home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13158 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  49 lines

  1. Path: news.iadfw.net!usenet
  2. From: Mark Nelson <markn@airmail.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: mutual data structures
  5. Date: Sat, 23 Mar 1996 15:52:50 -0600
  6. Organization: customer of Internet America
  7. Message-ID: <315472B2.2F2A@airmail.net>
  8. References: <4j1mu1$fas@baskerville.CS.Arizona.EDU>
  9. NNTP-Posting-Host: dal17-01.ppp.iadfw.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  14.  
  15. Koen De Bosschere wrote:
  16. > I am trying to create a data structure consisting of
  17. > two linked lists, with nodes pointing at each other
  18. > between the two lists.
  19. > The problem is that in order to create typed pointers, I
  20. > need the definition of the class, and no matter how I reorder
  21. > the class definitions, there will always be an undefined
  22. > class at some point.
  23.  
  24. > This kind of data structure
  25. > cannot be so unusual, so other people must have faced
  26. > this problem before.
  27.  
  28. You are right, it is a problem that has been dealt with. You need
  29. a simple forward declaration to inform the compiler of the existence
  30. of the class:
  31.  
  32. class node1;
  33.  
  34. class node2 {
  35.     node1 *n1;
  36. };
  37.  
  38. class node1 {
  39.     node2 *n2;
  40. };
  41.  
  42. The incomplete definition of node1 gives the compiler enough information
  43. to create a pointer, which is all you need at that point.
  44.  
  45. Mark Nelson
  46. http://web2.airmail.net/markn
  47.